package pw.hais.main.main_app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.squareup.okhttp.Response;

import pw.hais.http.base.OnHttpListener;
import pw.hais.main.R;
import pw.hais.main.main_app.app.BaseActivity;
import pw.hais.main.main_app.app.V;
import pw.hais.main.main_app.dao.LeanDao;
import pw.hais.main.main_app.model.UserModel;
import pw.hais.utils.EmptyUtil;
import pw.hais.utils.L;
import pw.hais.utils.SPUtils;

/**
 * 登录页面
 * Created by hais1992 on 2015/12/13.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private EditText edit_username,edit_password;
    private Button btn_login;
    private UserModel user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        user = new UserModel();

        initLayoutView();
    }

    private void initLayoutView() {
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_password = (EditText) findViewById(R.id.edit_password);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_reg) {
            startActivityForResult(new Intent(context, RegisterActivity.class), 100);
            return true;
        }else if (id == R.id.action_password) { //取回密码
            L.showShort("开发中，如有需要请联系作者！");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                verificationInfoAndPost();
                break;
        }
    }


    //验证信息
    private void verificationInfoAndPost() {
        user.password = edit_password.getText() == null ? "" : edit_password.getText().toString().trim();
        user.username = edit_username.getText() == null ? "" : edit_username.getText().toString().trim();
        if ("".equals(user.password) || "".equals(user.username)) {
            L.showShort("不能为空！");
        } else {
            doLoginUser();
        }
    }

    private void doLoginUser() {
        loadDialogShow("登录中...");
        LeanDao.doLoginUser(user, new OnHttpListener<UserModel>() {
            @Override
            public void onSuccess(Response response, UserModel data) {
                loadDialogDismiss();
                if(!EmptyUtil.emptyOfString(data.error)){
                    L.showShort(data.error+"");
                }else {
                    L.showShort("登录成功！");
                    app.user = data;
                    SPUtils.saveObject(V.SP.USER,data);
                    finish();
                }
            }
        });
    }
}
